$(function() {
    $.ajax({
        type: 'GET',
        url: 'https://kksappapi.kekeshi.com/api/app/v1/base/content_page?module=' + getUrlVal('module') + '&uuid=' + getUrlVal('uuid') + '&token=' + getUrlVal('token'),
        // // data to be added to query string:
        // data: { name: 'Zepto.js' },
        // type of data we are expecting in return:
        dataType: 'json',
        timeout: 300,
        context: $('body'),
        success: function(data){
            // Supposing this JSON payload was received:
            //   {"project": {"id": 42, "html": "<div>..." }}
            // append the HTML to context object.
            // this.append(data.project.html)
            $('.content').eq(0).html(data.data)
        },
        error: function(xhr, type){
            alert('Ajax error!')
        }
    })

    $.ajax({
        type: 'GET',
        url: 'https://kksappapi.kekeshi.com/api/app/v1/advert/base/get_single_advert?advert_space=' + getUrlVal('advert_space') + '&token=' + getUrlVal('token'),
        // // data to be added to query string:
        // data: { name: 'Zepto.js' },
        // type of data we are expecting in return:
        dataType: 'json',
        timeout: 300,
        context: $('body'),
        success: function(data){
            // Supposing this JSON payload was received:
            //   {"project": {"id": 42, "html": "<div>..." }}
            // append the HTML to context object.
            // this.append(data.project.html)
            console.log(data.data)
            $('.advert_img').eq(0).attr('src',data.data.advert.pic)
            $('.advert_link').eq(0).attr('href',data.data.advert.target_url)
        },
        error: function(xhr, type){
            alert('Ajax error!')
        }
    })



    function getUrlVal(para) {
        var search = location.search; //页面URL的查询部分字符串
        var arrPara = new Array(); //参数数组。数组单项为包含参数名和参数值的字符串，如“para=value”
        var arrVal = new Array(); //参数值数组。用于存储查找到的参数值

        if (search != "") {
            var index = 0;
            search = search.substr(1); //去除开头的“?”
            arrPara = search.split("&");

            for (i in arrPara) {
                var paraPre = para + "="; //参数前缀。即参数名+“=”，如“para=”
                if (arrPara[i].indexOf(paraPre) == 0 && paraPre.length < arrPara[i].length) {
                    arrVal[index] = decodeURI(arrPara[i].substr(paraPre.length)); //顺带URI解码避免出现乱码
                    index++;
                }
            }
        }

        if (arrVal.length == 1) {
            return arrVal[0];
        } else if (arrVal.length == 0) {
            return null;
        } else {
            return arrVal;
        }
    }

});
